using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ProgressionDisplayController:MonoBehaviour {

	[SerializeField] GameObject prefab;
	static Dictionary<string,float> datas = new Dictionary<string,float>();
	static Dictionary<string,float> lastDatas = new Dictionary<string,float>();
	static Dictionary<string,float> mergedDatas = new Dictionary<string,float>();

	List<ProgressionInstanceController> instances = new List<ProgressionInstanceController>();

	private void Start() {
		datas.Clear();
		lastDatas.Clear();
		mergedDatas.Clear();
	}

	public static void AddProgression(string name,float amount) {
		if(datas.ContainsKey(name)) datas[name]=amount;
		else datas.Add(name,amount);
	}

	private void Update() {

		mergedDatas.Clear();
		foreach(var d in datas) {
			mergedDatas.Add(d.Key,d.Value);
		}
		foreach(var d in lastDatas) {
			if(mergedDatas.ContainsKey(d.Key)) continue;
			mergedDatas.Add(d.Key,d.Value);
		}

		while(instances.Count<mergedDatas.Count) {
			GameObject newObject = Instantiate(prefab,transform.position,Quaternion.identity,transform);
			instances.Add(newObject.GetComponent<ProgressionInstanceController>());
		}
		int i = 0;
		foreach(var data in mergedDatas) {
			instances[i].SetValue(true,-14*i,data.Key,data.Value);
			i++;
		}
		for(;i<instances.Count;i++) {
			instances[i].SetValue(false,0,"0",0);
		}

		lastDatas=datas;
		datas=new Dictionary<string,float>();

	}

}
